/*
 * D.A.O. for JPA  
 * Created on 6 juin 2013 ( Time 15:03:48 )
 */

package org.demo.rapport.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.telosys.starterkits.strutsjpa.CoreUtils;
import org.telosys.starterkits.strutsjpa.GenericJpaDao;

import org.demo.rapport.bean.Rapport;

public class JpaRapportDAO extends GenericJpaDao<Rapport, Integer> {

	private static final Predicate[] ap = {};
	
	/**
	 * Constructs a DAO for Rapport
	 */
	public JpaRapportDAO() {
		super(Rapport.class);
	}
	
	/**
	 * Search by Rapport example
	 */
	public List<Rapport> search(final Rapport element) {
		final EntityManager em = getEntityManager();
		if ( element != null  ) {
			CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
			CriteriaQuery<Rapport> criteriaQuery = criteriaBuilder.createQuery(Rapport.class);
			Root<Rapport> from = criteriaQuery.from(Rapport.class);

			List<Predicate> listP = new ArrayList<Predicate>();


			if (CoreUtils.isNotBlank(element.getIdrapport())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idrapport"), element.getIdrapport());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getIdemploye())) {
				Predicate predicate = criteriaBuilder.equal(from.get("idemploye"), element.getIdemploye());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getTempspresence())) {
				Predicate predicate = criteriaBuilder.equal(from.get("tempspresence"), element.getTempspresence());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getDebutpresence())) {
				Predicate predicate = criteriaBuilder.equal(from.get("debutpresence"), element.getDebutpresence());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getFinpresence())) {
				Predicate predicate = criteriaBuilder.equal(from.get("finpresence"), element.getFinpresence());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getRetard())) {
				Predicate predicate = criteriaBuilder.equal(from.get("retard"), element.getRetard());
				listP.add(predicate);
			}
			if (CoreUtils.isNotBlank(element.getSortietot())) {
				Predicate predicate = criteriaBuilder.equal(from.get("sortietot"), element.getSortietot());
				listP.add(predicate);
			}
			
			criteriaQuery.where(listP.toArray(ap));
			TypedQuery<Rapport> tq = em.createQuery(criteriaQuery);
			return tq.getResultList();
		}
		else
		{
    		return this.loadAll();
		}
	}

}